<template>
    <div class="box">
        <bar1 title="分类"></bar1>
        <van-row>
            <van-col span="4">
                <van-sidebar v-model="active">
                    <van-sidebar-item :title="v.cat_name" v-for="(v, i) in list" :key="i"/>
                </van-sidebar>
            </van-col>
            <van-col span="18" offset="2">
                <van-pull-refresh v-model="loading" @refresh="onRefresh">
                    <div v-for="(value, index) in list[active]?.children" :key="index" >
                        <div>{{ value.cat_name }}</div>

                        <van-grid :border="false" :column-num="3">
                            <van-grid-item v-for="(v, i) in value.children" :key="i" @click="golist(v.cat_name)">
                                <van-image
                                    :src="v.cat_icon ? v.cat_icon : 'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg'" />
                                {{ v.cat_name }}
                            </van-grid-item>
                        </van-grid>
                    </div>
                </van-pull-refresh>
            </van-col>
        </van-row>
    </div>
</template>

<script setup>
import bar1 from '@/components/bar1.vue';
import { categoriesApi } from '@/api/api';
import { ref } from 'vue';
import { showToast } from 'vant';
import router from '@/router';
const active = ref(0);

let list = ref({})
const getlist = () => {
    categoriesApi().then((res) => {
        console.log(res);
        list.value = res.data.message
    })
}
getlist()

const count = ref(0);
const loading = ref(false);
const onRefresh = () => {
    setTimeout(() => {
        showToast('刷新成功');
        loading.value = false;
    }, 800);
};

const golist = (name) =>{
    router.push({path:'/search', query: name,name})
}
</script>

<style lang="scss" scoped>
.box {
    width: 100%;
    height: 100%;

}
</style>